﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using FisheryPlatform.Search.Core.Resource;

namespace FisheryPlatform.Search.Core.Analysis
{
    /// <summary>
    /// Load all supported dictionaries into a memory Hashtable.
    /// </summary>
    /// <author>gwd, 2006-6-12</author>
    public class DictionaryFactory
    {
        private System.Collections.Hashtable _dictionaries = null;

        public DictionaryFactory(CentralPath dictionaryRoot)
        {
            _dictionaries = new Hashtable();
            LoadDictionaries(dictionaryRoot.ToString());
        }

        /// <summary>
        /// Get a named dictionary. the param "name" stands for the path name of a dictionary.
        /// </summary>
        /// <param name="name">dictionary's path name</param>
        /// <returns>a Dictionary instance</returns>
        public Dictionary GetDictionary(string name)
        {
            return (Dictionary)_dictionaries[name.ToLower()];
        }

        /// <summary>
        /// Load all supported dictionaries in "dictionaryPath" into a memory Hashtable.
        /// </summary>
        /// <param name="dictionaryPath">dictionary path</param>
        private void LoadDictionaries(string dictionaryPath)
        {
            _dictionaries = new System.Collections.Hashtable();

            if (System.IO.Directory.Exists(dictionaryPath))
            {
                System.IO.DirectoryInfo dicDir = new System.IO.DirectoryInfo(dictionaryPath);
                System.IO.DirectoryInfo[] children = dicDir.GetDirectories();

                if (children != null)
                {
                    for (int i = 0; i < children.Length; i++)
                    {
                        System.IO.DirectoryInfo child = children[i];

                        string name = child.Name;
                        Dictionary dic = new FWLDictionary(name, child.FullName);
                        _dictionaries[name.ToLower()] = dic;
                        dic.LoadDictionary();
                    }
                }
            }
            //MMSupport.Debug(string.Format("Load dictionaries: {0} S", _loadTimeSpan.TotalSeconds));
        }
    }
}
